package com.shuqi.application;



import net.sqlcipher.database.SQLiteDatabase;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.shuqi.common.Config;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SqImageManager;
import com.shuqi.common.utils.Util;
import com.shuqi.database.dao.DataBaseManager;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 应用程序Application
 * 
 * @author
 * 
 */
public class ShuqiApplication extends Application {
    public static String packageName;
    private static ShuqiApplication mInstance;
    private static volatile Handler mApplicationHandler = null;
    /** 书旗是否正在运行 */
    private static boolean isShuQiRunning = false;


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("ShuqiApplication", "ShuqiApplication onCreate()");
        Log4an.init(this, Config.ROOT_PATH);
        Util.checkSig(this);// TODO 检查签名
        mInstance = this;
        System.loadLibrary("psProcess");
        SQLiteDatabase.loadLibs(this);
        DataBaseManager.getInstance().init(this);
        SqImageManager.init(this);
        StatisticsUtils.statisticsInit();
        if (ApplicationCrashHandler.RELEASE) {
            ApplicationCrashHandler crashHandler =
                    ApplicationCrashHandler.getCrashHandlerInstances();
            Thread.setDefaultUncaughtExceptionHandler(crashHandler);
        }
        packageName = getPackageName();
    }

    public ShuqiApplication() {
        if (mApplicationHandler == null) {
            mApplicationHandler = new Handler();
        }
    }

    public static Handler getApplicationHandler() {
        if (mApplicationHandler == null) {
            Looper.prepare();
            mApplicationHandler = new Handler();
            Looper.loop();
        }
        return mApplicationHandler;
    }

    public static Context getContext() {
        return mInstance;
    }

    public static ShuqiApplication getInstance() {
        return mInstance;
    }

    public static void callShuQiIsRunning() {
        isShuQiRunning = true;
    }

    public static boolean shuQiIsRunning() {
        return isShuQiRunning;
    }

    /** 退出软件时重置静态变量 */
    public static void onExit() {
        Log.e("ShuqiApplication", "onExit() 退出软件");
        isShuQiRunning = false;
        ApplicationCrashHandler.clear();
        Config.onExit();
    }

}
